//
//  Home_PlanVC.m
//  WanCaiC8
//
//  Created by tangang on 2018/3/12.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "Home_PlanVC.h"
#import "HomeTypeCell.h"
#import "HomeTypeCell2.h"
#import "PlanVC.h"

#import "Home_PlanCollectionTopView.h"
#import "HomeCollectionFooterView.h"
#import "MasterListVC.h"
#import "InformationVM.h"
#import "AdvertisingTextCell.h"
@interface Home_PlanVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic ,strong) UICollectionView *collectionView;

@property (nonatomic ,strong) NSArray           *advertisingArr;


//接收
@property (nonatomic ,strong) NSArray * arr;
@property (nonatomic ,strong) NSMutableArray * dataArr;
@property (nonatomic ,strong) NSMutableArray * dataArr2;


@property (nonatomic ,strong) NSMutableArray * arr3;
@property (nonatomic ,strong) NSMutableArray * arr4;
@property (nonatomic ,strong) NSMutableArray * arr1;
@property (nonatomic ,strong) NSMutableArray * arr2;



@end

@implementation Home_PlanVC{
    
}

-(void)loadData{
    
    self.arr = [InformationVM manage].homeDataArr.copy;
    
    [self.dataArr addObjectsFromArray:[LotteryNavModel mj_objectArrayWithKeyValuesArray:self.arr.firstObject]];
    [self.dataArr2 addObjectsFromArray:[LotteryNavModel mj_objectArrayWithKeyValuesArray:self.arr.lastObject]];
    
    
}
-(void)setRightItem{
    
    //bk导航烂右键
    UIBarButtonItem *rightItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"nav_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(onclickNav)];
    self.navigationItem.rightBarButtonItem=rightItem;
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.dataArr = [NSMutableArray array];
    self.dataArr2 = [NSMutableArray array];
    self.arr1 = [NSMutableArray array];
    self.arr2 = [NSMutableArray array];
    self.arr3 = [NSMutableArray array];
    self.arr4 = [NSMutableArray array];
    
    
    
    
    if (self.planType==PlanTypeMaster) {
        self.title=@"高手计划";
    }else{
        self.title=@"彩票计划";
    }
    
    
    [self setupUI];
    
    [self setRightItem];
    [self loadData];
    
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        [self getAdvertising];
     
    });
    
    
    
    
    
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [BaseVM manage].viewCtrl=self;
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

#pragma mark -- 获取广告
- (void)getAdvertising{
    
   
    
    WS(weakSelf);
    NSInteger type=6;
    if (self.planType==PlanTypeDefault) {
        type=9;
    }
    //    if ([BaseVM manage].city.length>0) {
    [[InformationVM manage] getAdvertWithLocation:@"0" andType:type Success:^(id returnValue) {
        
        
        [weakSelf.arr1 removeAllObjects];
        [weakSelf.arr2 removeAllObjects];
        [weakSelf.arr3 removeAllObjects];
        [weakSelf.arr4 removeAllObjects];

        for (AdvertisingModel *model in returnValue) {
            if (model.Layer == 3) {
                [weakSelf.arr3 addObject:model];
            }
            if (model.Layer == 4) {
                
                [weakSelf.arr4 addObject:model];
            }
            if (model.Layer == 1) {
                
                [weakSelf.arr1 addObject:model];
            }
            if (model.Layer == 2) {
                
                [weakSelf.arr2 addObject:model];
            }
        }
         [self.collectionView reloadData];
        
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
    //    }
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return self.arr.count;
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        
        return self.dataArr.count;
    }else{
        
        return self.dataArr2.count;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        
        HomeTypeCell2 *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"HomeTypeCell2" forIndexPath:indexPath];
        
        [cell setModel:self.dataArr[indexPath.row]];
        
        return cell;
        
        
    }else{
        
        HomeTypeCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"HomeTypeCell" forIndexPath:indexPath];
        
        
//        [cell setModel:self.dataArr2[indexPath.row]];
        
        return cell;
        
    }
    
    
    
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    LotteryNavModel *model;
    if (indexPath.section == 0) {
        
        model=self.dataArr[indexPath.row];
        
        if (self.planType==PlanTypeDefault) {
            PlanVC *vc=[[PlanVC alloc]init];
            LotteryTypeModel *typeModel=[[LotteryTypeModel alloc]init];
            typeModel.lType=model.LType;
            typeModel.Name=model.LTypeName;
            typeModel.Id=model.BigLType;
            vc.lotteryModel=typeModel;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            MasterListVC *vc=[[MasterListVC alloc]init];
            vc.lType = model.LType;
            vc.strTitle =model.LTypeName;
            [self.navigationController pushViewController:vc animated:YES];
        }
        
    }else{
        
        model = self.dataArr2[indexPath.row];
        if (self.planType==PlanTypeDefault) {
            PlanVC *vc=[[PlanVC alloc]init];
            LotteryTypeModel *typeModel=[[LotteryTypeModel alloc]init];
            typeModel.lType=model.LType;
            typeModel.Name=model.LTypeName;
            typeModel.Id=model.BigLType;
            vc.lotteryModel=typeModel;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            MasterListVC *vc=[[MasterListVC alloc]init];
            vc.lType = model.LType;
            vc.strTitle =model.LTypeName;
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        
    }
    
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        return CGSizeMake(SCREEN_WIDTH/2.0, SCREEN_WIDTH/4.0);
        
    }else{
        
        return CGSizeMake(SCREEN_WIDTH/3.0, SCREEN_WIDTH/4.0);
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return CGSizeMake(SCREEN_WIDTH, real_size(40));
        
    }else{
        return CGSizeMake(SCREEN_WIDTH, real_size(40+40*self.arr3.count));
    }
    
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section

{
    
    if (section==1) {
        return CGSizeMake(SCREEN_WIDTH, 40*self.arr4.count);
    }
    return CGSizeZero;
    
}


- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    if (kind == UICollectionElementKindSectionHeader)
    {
        
        if (indexPath.section==0) {
            
            Home_PlanCollectionTopView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Home_PlanCollectionTopView" forIndexPath:indexPath];
            [headerView setTitle:@"全国彩"];
            
            [headerView setAdvertising:self.arr1.lastObject];
            
            return headerView;
        }else{
            
            Home_PlanCollectionTopView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Home_PlanCollectionTopView2" forIndexPath:indexPath];
            [headerView setTitle:@"高频彩"];
            
            [headerView setAdvertising:self.arr2.lastObject];
            [headerView setAdModelArr:self.arr3];
            return headerView;
            
        }
    }else if (kind == UICollectionElementKindSectionFooter){
        
        if (indexPath.section == 1) {
            HomeCollectionFooterView *footerView =  [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"HomeCollectionFooterView" forIndexPath:indexPath];
            
            [footerView setAdModelArr:self.arr4];
            
            return footerView;
        }
        
        
    }
    
    return nil;
}

#pragma mark -- setupUI
-(void)setupUI{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    [layout setItemSize:CGSizeMake(SCREEN_WIDTH/4.0, SCREEN_WIDTH/4.0)];
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    layout.minimumInteritemSpacing = 0.0;
    layout.minimumLineSpacing = 0.0;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor=[UIColor whiteColor];
    _collectionView.showsVerticalScrollIndicator=NO;
    [_collectionView registerClass:[HomeTypeCell class] forCellWithReuseIdentifier:@"HomeTypeCell"];
    [_collectionView registerClass:[HomeTypeCell2 class] forCellWithReuseIdentifier:@"HomeTypeCell2"];
    
    
    
    [_collectionView registerClass:[Home_PlanCollectionTopView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Home_PlanCollectionTopView"];
    [_collectionView registerClass:[Home_PlanCollectionTopView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Home_PlanCollectionTopView2"];
    
    [_collectionView registerClass:[HomeCollectionFooterView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"HomeCollectionFooterView"];
    
    [self.view addSubview:_collectionView];
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.top.right.equalTo(self.view);
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end


